package org.sg20.searchengine.search;

import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import org.sg20.parsename.ParseNameOutputKey;

import java.io.IOException;
import java.util.HashMap;

public class SearchMapper extends Mapper<LongWritable, Text, Text, Text> {
    HashMap<String, Integer> hmap;
    @Override
    protected void setup(Mapper<LongWritable, Text, Text, Text>.Context context) {
        String inputstr = context.getConfiguration().getStrings("input")[0];
        String[] strs = inputstr.split(" ");
        hmap = new HashMap<>();
        for(String str: strs){
            System.out.println(str);
            hmap.put(str, 1);
        }
    }


    @Override
    protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, Text>.Context context) throws IOException, InterruptedException {
        String[] strs = value.toString().split("\t");


        if(hmap.containsKey(strs[0])){
            for(int i = 1; i < strs.length; i++){
                //System.out.println(strs[0]);
                context.write(new Text(strs[i]), new Text(strs[0]));
            }
        }
    }
}
